package com.bossien.flink.common;

import com.bossien.flink.annatation.JHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 胡春林
 */
@Slf4j
@Component
public class JHandlerBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

       JHandler jHandler = AnnotationUtils.findAnnotation(bean.getClass(), JHandler.class);
       if(jHandler != null)
       {
           Method[] methods = bean.getClass().getDeclaredMethods();
           if(methods != null)
           {
               for (Method method : methods)
               {
              // try {
                   if(method.getReturnType() instanceof Object && method.getReturnType().getName().indexOf("Object") > 0)
                       continue;
                   if (method.getName().equals("handler")) {

                       log.info("业务调用作业名称:{}",jHandler.value());
                       //method.invoke(bean, null);

                       }
//                   }catch (IllegalAccessException e)
//                   {
//                      log.info("访问method方法失败！{}",beanName+method.getName());
//
//                   }catch (InvocationTargetException ex)
//                   {
//                      log.info("invoke 调用失败{}",beanName+method.getName());
//
//                   }
               }
           }
       }

       return bean;

    }
}
